package com.icim.util;

import static com.icim.define.CommDefine.TRANSATION_MODE_BOTH_PPT_HIS;
import static com.icim.define.CommDefine.TRANSATION_MODE_HIS;
import static com.icim.define.CommDefine.TRANSATION_MODE_NONE;
import static com.icim.define.CommDefine.TRANSATION_MODE_PPT;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.icim.common.LoadHistoryConfig;
import com.icim.table.TableConfig;
import redis.clients.jedis.Jedis;

@Component("tableConfigUtil")
public class TableConfigUtil {
	private static Logger logger = Logger.getLogger(TableConfigUtil.class);
	private final String PPT_SUFFIXES = ".PPT";
	private final String HIS_SUFFIXES = ".HIS";
	@Resource
	private LoadHistoryConfig loadHistoryConfig;

	public static void main(String[] args) {
		TableConfigUtil confUtil = new TableConfigUtil();
		confUtil.loadHistoryConfig = new LoadHistoryConfig();
		confUtil.loadHistoryConfig.setHistoryIniFile("/opt/icim/conf/jrep_1.properties");
		TableConfig tableConfig = confUtil.getConfig("RET_CUS_COMPL_RPT");
	}

	public TableConfig getConfig(String _opeTableName) {
		String hisTblFile = loadHistoryConfig.getHistoryIniFile();
		TableConfig tableConfig = null;//return value
		Properties hisProperties = new Properties();
		String _filePptTableName = null;
		String _fileHisTableName = null;
		_opeTableName = _opeTableName.toUpperCase();


		//Redis 读取
		/*Jedis jedis = new Jedis("101.132.68.201", 6379);
//		Jedis jedis = new Jedis("localhost", 6379);

		byte[] tableConfigBytes = jedis.get(("lodhis_config_" + _opeTableName).getBytes());
		try {
			tableConfig = (TableConfig) ObjectTranscoder.deserialize(tableConfigBytes);
		} catch (IOException e) {
			e.printStackTrace();
		}
		boolean hasToSendHis ;
		hasToSendHis = tableConfig.isHasToSendHis();
		if (hasToSendHis){
			_filePptTableName =tableConfig.getPptTableName();
			_fileHisTableName =tableConfig.getHisTableName();
		}*/

		try {
			hisProperties.load(new FileInputStream(hisTblFile));
			_filePptTableName = hisProperties.getProperty(_opeTableName + PPT_SUFFIXES);
			_fileHisTableName = hisProperties.getProperty(_opeTableName + HIS_SUFFIXES);
			tableConfig = new TableConfig();
			tableConfig.setOpeTableName(_opeTableName);
			tableConfig.setPptTableName(_filePptTableName);
			tableConfig.setHisTableName(_fileHisTableName);

			if (_filePptTableName != null && _fileHisTableName == null) {
				tableConfig.setTransactionMode(TRANSATION_MODE_PPT);

			} else if (_filePptTableName == null && _fileHisTableName != null) {
				tableConfig.setTransactionMode(TRANSATION_MODE_HIS);

			} else if (_filePptTableName != null && _fileHisTableName != null) {
				tableConfig.setTransactionMode(TRANSATION_MODE_BOTH_PPT_HIS);

			} else {
				tableConfig.setTransactionMode(TRANSATION_MODE_NONE);
				logger.error("can not find "+_opeTableName+"in configFile:"+hisTblFile+";Check");
			}
		} catch (Exception e) {
			logger.error("读取配置档出错:" + e.getMessage());
			e.printStackTrace();
		}
		return tableConfig;

	}

}
